package top.hcode.hoj.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.nutz.lang.Lang;
import org.sagacity.sqltoy.model.Page;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author orangej
 * @since 2024/12/29
 */
public class PageUtil {

    public static <T> IPage<T> toMybatis(Page<T> page) {
        IPage<T> result = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page.getPageNo(), page.getPageSize());
        result.setRecords(page.getRows());
        result.setTotal(page.getRecordCount());

        return result;
    }

    public static void fillAscDescColumns(Map<String, Object> params, List<OrderItem> orderItems) {
        if (Lang.isEmpty(orderItems)) {
            return;
        }

        String asc = orderItems.stream().filter(OrderItem::isAsc).map(OrderItem::getColumn)
                .collect(Collectors.joining(","));
        String desc = orderItems.stream().filter(o -> !o.isAsc()).map(OrderItem::getColumn)
                .collect(Collectors.joining(","));
        if (Lang.isNotEmpty(asc)) {
            params.put("ascColumns", asc);
        }
    }

}
